package com.business.business.web.service;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.business.business.common.base.BaseService;
import com.business.business.model.BusinessBasisinfoFiles;
import com.business.business.model.BusinessLegaldocuments;
import com.business.business.support.model.RespEntity;
import com.business.business.support.mybatisplus.QueryWrapper;
import com.business.business.support.utils.requestutil.HttpRequestUtil;
import com.business.business.web.mapper.BusinessBasisinfoFilesMapper;
import com.business.business.web.mapper.BusinessLegaldocumentsMapper;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
@Slf4j
public class LegaldocumentsService extends BaseService {
    private static final Logger logger = LoggerFactory.getLogger(LegaldocumentsService.class);

    @Resource
    private BusinessLegaldocumentsMapper businessLegaldocumentsMapper;
    @Resource
    private BusinessBasisinfoFilesMapper businessBasisinfoFilesMapper;

    public RespEntity legaldocumentsAdd(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());

        com.alibaba.fastjson.JSONObject token = makeDataForToken(jsonParam,"systemsetup","config","getDecodeToken","systemsetup");
        //调用token验证接口
        if(StrUtil.isNotBlank(token.getString("methodCodeFlag"))) return RespEntity.expire();
        if(StrUtil.isNotBlank(token.getString("methodErrorFlag"))) return RespEntity.fail(token.getString("msg"));
        String home_id = token.getString("home_id");
        String userId = token.getString("user_id");
        String userType = token.getString("user_type");
        String businessId = "";
        if ("1".equals(userType)){
            businessId = jsonParam.getStr("businessId");
        }else {
            businessId = home_id;
        }
        //获取前台的数据
        String remarks = jsonParam.getStr("remarks");
        String type = jsonParam.getStr("type");
        String fileName = jsonParam.getStr("fileName");
        String filePath = jsonParam.getStr("filePath");
        //自动生成ID
        String uuid = UUID.randomUUID().toString();
        //自动生成maketime
        DateTime date = DateUtil.date();
//        String makingTime = DateUtil.now();
        String createyear = DateUtil.year(date)+"";
        BusinessLegaldocuments businessLegaldocuments = new BusinessLegaldocuments();
        businessLegaldocuments.setId(uuid);
        businessLegaldocuments.setType(type);
        businessLegaldocuments.setBusinessId(businessId);
        businessLegaldocuments.setCreatetime(date);
        businessLegaldocuments.setCreateyear(createyear);
        businessLegaldocuments.setRemarks(remarks);
        boolean businessLegaldocumentsInsert = businessLegaldocumentsMapper.insert(businessLegaldocuments)>0?true:false;
        BusinessBasisinfoFiles businessBasisinfoFiles = new BusinessBasisinfoFiles();
            businessBasisinfoFiles.setBusinessBasisinfoId(uuid);
            businessBasisinfoFiles.setFileName(fileName);
            businessBasisinfoFiles.setFilePath(filePath);
        boolean insertBusinessBasisinfoFiles= businessBasisinfoFilesMapper.insert(businessBasisinfoFiles)>0?true:false;;

        if (insertBusinessBasisinfoFiles && insertBusinessBasisinfoFiles) {
            return RespEntity.SUCCESS;
        } else {
            return RespEntity.fail("插入失败");
        }
    }

    /*
     * @Author LiHaoDong
     * @Description 合法性文件列表
     * @Date 14:56 2020/7/18
     * @Param [jsonParam]
     * @return com.business.business.support.model.RespEntity
     **/
    public RespEntity legaldocumentsListPage(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());

        com.alibaba.fastjson.JSONObject token = makeDataForToken(jsonParam,"systemsetup","config","getDecodeToken","systemsetup");
        //调用token验证接口
        if(StrUtil.isNotBlank(token.getString("methodCodeFlag"))) return RespEntity.expire();
        if(StrUtil.isNotBlank(token.getString("methodErrorFlag"))) return RespEntity.fail(token.getString("msg"));
        String home_id = token.getString("home_id");
        String userId = token.getString("user_id");
        String userType = token.getString("user_type");
        if ("2".equals(userType)){
            jsonParam.put("businessId",home_id);
        }
        //* current 第几页
        // * size 每页显示几条
        Page<BusinessLegaldocuments> page = new Page<>(jsonParam.getInt("page"), jsonParam.getInt("limit"));
        //调用自定义sql
        IPage<BusinessLegaldocuments> iPage = businessLegaldocumentsMapper.legaldocumentsListPage(page, jsonParam);

        List<BusinessLegaldocuments> businessLegaldocumentsList = iPage.getRecords();

        Map<String, Object> data = new HashMap<>();
        data.put("totalSize", iPage.getTotal());
        data.put("pages", iPage.getPages());
        data.put("pageSize", jsonParam.getInt("limit"));
        data.put("Items",businessLegaldocumentsList);
        data.put("currentPage",jsonParam.getInt("page"));
        data.put("currentSize",jsonParam.getInt("limit"));
        return RespEntity.success(data);
    }

    /*
     * @Author LiHaoDong
     * @Description 合法性文件详情
     * @Date 15:20 2020/7/18
     * @Param [jsonParam]
     * @return com.business.business.support.model.RespEntity
     **/
    public RespEntity legaldocumentsDetail(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        List<BusinessLegaldocuments> businessLegaldocumentsList = this.businessLegaldocumentsMapper.legaldocumentsDetail(jsonParam);
        if (!businessLegaldocumentsList.isEmpty()) {
            return RespEntity.success(businessLegaldocumentsList);
        }
        return RespEntity.fail("查询失败");
    }

    /*
     * @Author LiHaoDong
     * @Description 合法性文件修改
     * @Date 16:04 2020/7/18
     * @Param [jsonParam]
     * @return com.business.business.support.model.RespEntity
     **/
    public RespEntity legaldocumentsUpdate(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());

        com.alibaba.fastjson.JSONObject token = makeDataForToken(jsonParam,"systemsetup","config","getDecodeToken","systemsetup");
        //调用token验证接口
        if(StrUtil.isNotBlank(token.getString("methodCodeFlag"))) return RespEntity.expire();
        if(StrUtil.isNotBlank(token.getString("methodErrorFlag"))) return RespEntity.fail(token.getString("msg"));
        String home_id = token.getString("home_id");
        String userId = token.getString("user_id");
        String userType = token.getString("user_type");
        String remarks = jsonParam.getStr("remarks");
        String type = jsonParam.getStr("type");
        String fileName = jsonParam.getStr("fileName");
        String id = jsonParam.getStr("id");
        String filePath = jsonParam.getStr("filePath");
        QueryWrapper queryWrapperBusinessBasisinfoFiles = new QueryWrapper();
        queryWrapperBusinessBasisinfoFiles.eq("business_basisinfo_id", id);
        int delete = this.businessBasisinfoFilesMapper.delete(queryWrapperBusinessBasisinfoFiles);
        BusinessLegaldocuments businessLegaldocuments = new BusinessLegaldocuments();
        businessLegaldocuments.setRemarks(remarks);
        businessLegaldocuments.setType(type);
        QueryWrapper queryWrapperBusinessLegaldocuments = new QueryWrapper();
        queryWrapperBusinessLegaldocuments.eq("id", id);
        int update = this.businessLegaldocumentsMapper.update(businessLegaldocuments, queryWrapperBusinessLegaldocuments);
        BusinessBasisinfoFiles businessBasisinfoFiles = new BusinessBasisinfoFiles();
        businessBasisinfoFiles.setBusinessBasisinfoId(id);
        businessBasisinfoFiles.setFileName(fileName);
        businessBasisinfoFiles.setFilePath(filePath);
        int insert = this.businessBasisinfoFilesMapper.insert(businessBasisinfoFiles);
        if (delete > 0 && update > 0 && insert > 0) {
            return RespEntity.success();
        }
        return RespEntity.fail("更新失败");
    }

    /**
     * @Author LiHaoDong
     * @Description 合法性文件删除
     * @Date 16:22 2020/7/18
     * @Param [jsonParam]
     * @return com.business.business.support.model.RespEntity
     **/
    public RespEntity legaldocumentsDelete(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());

        com.alibaba.fastjson.JSONObject token = makeDataForToken(jsonParam,"systemsetup","config","getDecodeToken","systemsetup");
        //调用token验证接口
        if(StrUtil.isNotBlank(token.getString("methodCodeFlag"))) return RespEntity.expire();
        if(StrUtil.isNotBlank(token.getString("methodErrorFlag"))) return RespEntity.fail(token.getString("msg"));
        String home_id = token.getString("home_id");
        String id = jsonParam.getStr("id");
        int deleteById = this.businessLegaldocumentsMapper.deleteById(id);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("business_basisinfo_id", id);
        int delete = this.businessBasisinfoFilesMapper.delete(queryWrapper);
        if (deleteById > 0 && delete > 0) {
            return RespEntity.success();
        }
        return RespEntity.fail("删除失败");
    }
}
